package com.leyou.goods.service;

import com.leyou.goods.client.*;
import com.leyou.item.pojo.*;
import com.leyou.search.pojo.Goods;
import com.leyou.search.pojo.SearchRequest;
import com.leyou.goods.client.BrandClient;
import com.leyou.goods.client.CategoryClient;
import com.leyou.goods.client.GoodsClient;
import com.leyou.goods.client.SpecClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GoodsService {

    @Autowired
    private BrandClient brandClient;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecClient specClient;

    @Autowired
    private SearchClient searchClient;

    private static final Logger logger = LoggerFactory.getLogger(GoodsService.class);


    public Map<String,Object> querySpuById(Long id){
        try {
            Map<String, Object> map = new HashMap<>();
            //查询spu
            Spu spu = this.goodsClient.querySpuById(id);
            //查询spu详情
            ResponseEntity<SpuDetail> de = this.goodsClient.querySpuDetailById(id);
            SpuDetail spuDetail = de.getBody();
            //查询skus
            ResponseEntity<List<Sku>> sku = this.goodsClient.querySkuBySpuId(id);
            List<Sku> skus = sku.getBody();
            //查询品牌
            Brand brand = this.brandClient.queryBrandByBid(spu.getBrandId());
            //查找相关品牌
            List<Brand> relatedBrands = brandClient.queryBrandByCid(spu.getCid3()).getBody();
            //查询分类
            List<Long> longs = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
            ResponseEntity<List<String>> cnamess = this.categoryClient.queryNameByIds(longs);
            List<String> cnames = cnamess.getBody();
            ArrayList<Category> categories = new ArrayList<>();
            for (int x = 0; x < 3; x++) {
                Category category = new Category();
                category.setId(longs.get(x));
                category.setName(cnames.get(x));
                categories.add(category);
            }
            //查询相关分类
            List<Category> c2List = categoryClient.queryCategoryListByParentId(spu.getCid1());
            List<Category> relatedCategories = new ArrayList<>();
            c2List.forEach(c2->{
                List<Category> list = categoryClient.queryCategoryListByParentId(c2.getId());
                relatedCategories.addAll(list);
            });
            //查询相关商品
            List<SpuBo> relatedGoods = goodsClient.querySpuBosByCidAndNum(spu.getCid1(), 5);
            //查询配件商品
            List<SpuBo> partGoods = goodsClient.querySpuBosByCidAndNum(83L,5);
            //查询你可能喜欢
            SearchRequest request = new SearchRequest();
            request.setPage(1);
            request.setSize(6);
            String key = "";
            for (String name : cnames) {
                key = key + name;
            }
            request.setKey(key);
            List<Goods> loveGoods = searchClient.guessYouLove(request);
            //查询组类参数
            List<SpecGroup> specGroups = this.specClient.querySpecsByCid(spu.getCid3());
            //查询特有参数
            List<SpecParam> params = this.specClient.querySpecParamByGid(null, spu.getCid3(), null, false);
            //处理规格参数
            HashMap<Long, Object> paramMap = new HashMap<>();
            params.forEach(p -> {
                paramMap.put(p.getId(), p.getName());
            });

            //封装spu
            map.put("spu", spu);
            //封装详情
            map.put("spuDetail", spuDetail);
            //封装sku
            map.put("skus", skus);
            //封装品牌
            map.put("brand", brand);
            //封装标签
            map.put("categories", categories);
            //封装specgroup
            map.put("specGroups", specGroups);
            //封装params
            map.put("paramMap", paramMap);
            //封装相关品牌
            map.put("relatedBrands",relatedBrands);
            //封装相关商品
            map.put("relatedGoods",relatedGoods);
            //封装配件商品
            map.put("partGoods",partGoods);
            //封装猜你喜欢的商品
            map.put("loveGoods",loveGoods);
            //封装相关分类
            map.put("relatedCategories",relatedCategories);

            return map;
        }catch (Exception e){
            logger.error("加载商品数据出错,spuId:{}",  e);
            e.printStackTrace();
        }
        return null;
    }


}
